/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package quanlysieuthi;
import DAO.*;
import DTO.*;
import Model.CModel;
import java.awt.Point;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Vector;
import javax.swing.JOptionPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;
/**
 *
 * @author quangphuc
 */
public class FrmNhanVien extends javax.swing.JFrame {

    /**
     * Creates new form FrmNhanVien
     */
    public FrmNhanVien() {
        initComponents();
        this.dtcNgay_sinh.setDateFormatString(QuanLySieuThi.formatDate);
        this.lnhanvien = new ArrayList<CNhanVien>();
        loadCombobox();
        loadData();
    }
    public FrmNhanVien(long userId,String hoten){                        
        initComponents();
        this.user_id = userId;
        this.lnhanvien = new ArrayList<CNhanVien>();
        loadCombobox();
        loadData();        
        this.add = true;
        this.edit = false;
        this.btnadd.setEnabled(false);
        this.btnedit.setEnabled(false);
        this.btnsave.setEnabled(true);
        this.clearText();
        defaultValue();
        this.setEditable(true);
        this.txtTenNV.setText(hoten);
        this.txtTenNV.setEditable(false);
    }            
    private List<CNhanVien> lnhanvien;
    private List<User> luser = new ArrayList<User>();
    private List<CChuc_vu> lchucvu = new ArrayList<CChuc_vu>();
    
    private CChuc_VuDAO chuc_vuDAO = new CChuc_VuDAO();
    private CNhanVienDAO nhan_vienDAO = new CNhanVienDAO();
    private UserDAO  userDAO = new UserDAO();
    SubConfig config = null;
    private Boolean add = false;
    private Boolean edit = false;
    private Boolean delete = false;
    private int currentIndex = 0;
    private int numberRow = 0;
    long id = -1;
    long user_id = -1;
    private void loadCombobox(){
        config = SubConfig.getInstance();
        config.init();
        chuc_vuDAO.sWhere = " order by id asc";
        lchucvu = chuc_vuDAO.getAll(config);
        
        userDAO.sWhere =" where user_id not in(select id from users)";
        luser = userDAO.getAll(config);        
        config.close();        
        this.cbbchuc_vu_id.removeAllItems();
        for(CChuc_vu item : lchucvu){
            this.cbbchuc_vu_id.addItem(item.getTenChucVu());
        }
                  
    }
    
    private void defaultValue(){
        CNhanVien temp = null;
        String scode = "nv0001";
        if(lnhanvien.size() > 0){
            temp = lnhanvien.get(0);
            scode = temp.getMaNhanVien();
            scode = CKiemTra.MaTuTang(scode);
        }
        this.txtMaNV.setText(scode);
        this.txtMaNV.setEditable(false);
        dtcNgay_sinh.setDate(new Date());
    }
    private void loadData (){
        CModel _model = new CModel((QuanLySieuThi.user_name));
        //lnhanvien = nhan_vienDAO.getAll(config);
        lnhanvien = _model.getNhanVien();        
        Vector<Vector> data = new Vector<Vector>();
        for(CNhanVien nv : lnhanvien){
            Vector<String> row = new Vector<String>();
            row.addElement(nv.getMaNhanVien());
            row.addElement(nv.getHoTen());
            row.addElement(nv.getDiaChi());
            row.addElement(nv.getDienThoai());
            String strGioiTinh = nv.getGioiTinh() == true ? "Nam" :"Nữ";
            row.addElement(strGioiTinh);
            data.add(row);
        }
        Vector<String> columnNames = new Vector<String>();
        columnNames.addElement("Mã NV");
        columnNames.addElement("Họ Tên");
        columnNames.addElement("Địa chỉ");        
        columnNames.addElement("Điện thoại");
        columnNames.addElement("Giới tính");
        DefaultTableModel model = new DefaultTableModel(data, columnNames);
        txtRows.setText("0/0");
        this.tbldanh_sach.removeAll();
        this.tbldanh_sach.setModel(model);
        this.tbldanh_sach.setFillsViewportHeight(true);
        this.setEnabled(true);
        this.currentIndex = 0;       
        this.numberRow = lnhanvien.size();
        this.view(this.currentIndex);
        this.setEditable(false);
    }
    
    private void setEditable(Boolean b){
        this.txtMaNV.setEditable(b);
        this.txtTenNV.setEditable(b);
        this.cbbchuc_vu_id.setEditable(b);        
        this.txtDiaChi.setEditable(b);
        this.chkGioi_tinh.setEnabled(b);
        this.txtDienThoai.setEditable(b);
        this.txtCmnd.setEditable(b);
    }
    private void clearText(){
        this.txtMaNV.setText("");
        this.txtTenNV.setText("");
        this.txtDiaChi.setText("");
        this.txtDienThoai.setText("");
        this.txtCmnd.setText("");
        this.chkGioi_tinh.setState(false);
    }
    private Boolean isEmpty(){
        //kiem tra o nhan vien nhap lieu chua
        if(this.txtMaNV.getText().equals("")){
            JOptionPane.showMessageDialog(this, String.format("Vui lòng nhập mã nhân viên"));
            return true;
        }
        if(this.txtTenNV.getText().equals("")){
            JOptionPane.showMessageDialog(this, String.format("Vui lòng nhập tên nhân viên"));
            return true;
        }
        if(this.txtDiaChi.getText().equals("")){
            JOptionPane.showMessageDialog(this, String.format("Vui lòng nhập địa chỉ"));
            return true;
        }
        
        if(this.txtCmnd.getText().length() !=9){
            JOptionPane.showMessageDialog(this, String.format("Vui lòng nhập CMND phải đủ 9 ký tự"));
            return true;
        }
        return false;
    }
    
    private Boolean isExists(){
        //Kiem tra ma nhan vien ton tai chua
        Boolean flag = nhan_vienDAO.Exists(CFields.ma_nhan_vien, this.txtMaNV.getText(), CFields.tb_nhan_vien, config);
        if(flag){
            /// goi cau thong bao hien len
            JOptionPane.showMessageDialog(this, String.format("Ma nhan vien %s nay da ton tai.", this.txtMaNV.getText()));
            return true;
        }
        flag = nhan_vienDAO.Exists(CFields.cmnd, this.txtCmnd.getText(), CFields.tb_nhan_vien, config);
        return false;
    }
    private void view(int index){
        id = -1;
        if(index >=0 && index <  this.numberRow){
            CNhanVien nv = lnhanvien.get(index);
            this.txtDiaChi.setText(nv.getDiaChi());
            this.txtDienThoai.setText(nv.getDienThoai());
            this.txtMaNV.setText(nv.getMaNhanVien());
            this.txtTenNV.setText(nv.getHoTen());
            this.dtcNgay_sinh.setDate(nv.getNgaySinh());            
            this.chkGioi_tinh.setState(nv.getGioiTinh());
            this.tbldanh_sach.setRowSelectionInterval(index, index);
            id = nv.getId();
            txtRows.setText(String.format("%s/%s", index+1,numberRow));
        }
    }
    
    private Boolean addNew(){
        
        if(this.isEmpty() || this.isExists()){
            return false;
        }        
        String ma_nhan_vien = this.txtMaNV.getText();
        String ho_ten = this.txtTenNV.getText();
        String dia_chi = this.txtDiaChi.getText();
        String dien_thoai = this.txtDienThoai.getText();
        Date ns = dtcNgay_sinh.getDate();
        Boolean gioi_tinh = chkGioi_tinh.getState();
        //user_id
        //int chuc_vu = this.cbbchuc_vu_id.get//
        int selectIndex = this.cbbchuc_vu_id.getSelectedIndex();
        int chuc_vu = -1;
        if(selectIndex >=0 && selectIndex < this.lchucvu.size()){
            CChuc_vu  cv = lchucvu.get(selectIndex);
            chuc_vu = (int)cv.getId();
        }
        CModel _model = new CModel(QuanLySieuThi.user_name);
        _model.insertNhanVien(user_id, ho_ten, dia_chi, dien_thoai, chuc_vu, gioi_tinh, ma_nhan_vien, ns);
        _model.saveHisTory();
        _model.Close();
        this.loadData();
        return true;
    }
    
    private Boolean EditRow(){
        String ma_nhan_vien = this.txtMaNV.getText();
        String ho_ten = this.txtTenNV.getText();
        String dia_chi = this.txtDiaChi.getText();
        String dien_thoai = this.txtDienThoai.getText();
        Date ns = dtcNgay_sinh.getDate();
        Boolean gioi_tinh = chkGioi_tinh.getState();
        //user_id
        //int chuc_vu = this.cbbchuc_vu_id.get//
        int selectIndex = this.cbbchuc_vu_id.getSelectedIndex();
        int chuc_vu = -1;
        if(selectIndex >=0 && selectIndex < this.lchucvu.size()){
            CChuc_vu  cv = lchucvu.get(selectIndex);
            chuc_vu = (int)cv.getId();
        }
        CModel _model = new CModel(QuanLySieuThi.user_name);
       // _model.up insertNhanVien(user_id, ho_ten, dia_chi, dien_thoai, chuc_vu, gioi_tinh, ma_nhan_vien, ns);
        _model.updateNhanVien(user_id, ho_ten, dia_chi, dien_thoai, chuc_vu, gioi_tinh, ma_nhan_vien, ns, id);
        _model.saveHisTory();
        _model.Close();
        this.loadData();
        return true;
    }
    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel7 = new javax.swing.JLabel();
        jPanel3 = new javax.swing.JPanel();
        jLabel16 = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        txtMaNV = new javax.swing.JTextField();
        txtTenNV = new javax.swing.JTextField();
        jLabel18 = new javax.swing.JLabel();
        jLabel19 = new javax.swing.JLabel();
        txtDiaChi = new javax.swing.JTextField();
        jLabel20 = new javax.swing.JLabel();
        jLabel21 = new javax.swing.JLabel();
        txtDienThoai = new javax.swing.JTextField();
        jLabel22 = new javax.swing.JLabel();
        cbbchuc_vu_id = new javax.swing.JComboBox();
        dtcNgay_sinh = new com.toedter.calendar.JDateChooser();
        chkGioi_tinh = new java.awt.Checkbox();
        jLabel1 = new javax.swing.JLabel();
        txtCmnd = new javax.swing.JTextField();
        btnadd = new javax.swing.JButton();
        btnedit = new javax.swing.JButton();
        btnsave = new javax.swing.JButton();
        btndelete = new javax.swing.JButton();
        btnclose = new javax.swing.JButton();
        jPanel2 = new javax.swing.JPanel();
        jButton6 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        txtRows = new javax.swing.JTextField();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jScrollPane1 = new javax.swing.JScrollPane();
        tbldanh_sach = new javax.swing.JTable();

        jLabel7.setFont(new java.awt.Font("Tahoma", 1, 18)); // NOI18N
        jLabel7.setForeground(new java.awt.Color(102, 102, 255));
        jLabel7.setText("QUẢN LÝ NHÂN VIÊN");

        jPanel3.setBorder(javax.swing.BorderFactory.createTitledBorder("Thông tin nhân viên"));

        jLabel16.setText("Mã nhân viên: ");

        jLabel17.setText("Tên nhân viên:");

        jLabel18.setText("Giới tính:");

        jLabel19.setText("Địa chỉ:");

        jLabel20.setText("Điện thoại:");

        jLabel21.setText("Ngày sinh:");

        jLabel22.setText("Chức vụ:");

        cbbchuc_vu_id.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "Item 1", "Item 2", "Item 3", "Item 4" }));

        dtcNgay_sinh.setDateFormatString("dd-MM-yyyy");

        chkGioi_tinh.setLabel("Nam");

        jLabel1.setText("CMND :");

        javax.swing.GroupLayout jPanel3Layout = new javax.swing.GroupLayout(jPanel3);
        jPanel3.setLayout(jPanel3Layout);
        jPanel3Layout.setHorizontalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGap(22, 22, 22)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                        .addComponent(jLabel17)
                        .addComponent(jLabel16))
                    .addComponent(jLabel22))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(txtTenNV, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(cbbchuc_vu_id, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel18)
                    .addComponent(jLabel19))
                .addGap(26, 26, 26)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, 150, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(chkGioi_tinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(31, 31, 31)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(jLabel20)
                    .addComponent(jLabel21)
                    .addComponent(jLabel1))
                .addGap(18, 18, 18)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                    .addComponent(txtDienThoai, javax.swing.GroupLayout.DEFAULT_SIZE, 150, Short.MAX_VALUE)
                    .addComponent(dtcNgay_sinh, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(txtCmnd))
                .addContainerGap(21, Short.MAX_VALUE))
        );
        jPanel3Layout.setVerticalGroup(
            jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel3Layout.createSequentialGroup()
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGap(3, 3, 3)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel16)
                            .addComponent(txtMaNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel17)
                            .addComponent(txtTenNV, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                            .addComponent(jLabel18)
                            .addComponent(chkGioi_tinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel19)
                            .addComponent(txtDiaChi, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanel3Layout.createSequentialGroup()
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel20)
                            .addComponent(txtDienThoai, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel21)
                            .addComponent(dtcNgay_sinh, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel3Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel22)
                    .addComponent(cbbchuc_vu_id, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel1)
                    .addComponent(txtCmnd, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap())
        );

        btnadd.setText("Thêm mới");
        btnadd.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnaddActionPerformed(evt);
            }
        });

        btnedit.setText("Sửa");
        btnedit.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btneditActionPerformed(evt);
            }
        });

        btnsave.setText("Lưu");
        btnsave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnsaveActionPerformed(evt);
            }
        });

        btndelete.setText("Xóa");
        btndelete.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btndeleteActionPerformed(evt);
            }
        });

        btnclose.setText("Thoát");
        btnclose.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btncloseActionPerformed(evt);
            }
        });

        jPanel2.setBorder(javax.swing.BorderFactory.createTitledBorder("Danh sách nhân viên"));

        jButton6.setText("<<");
        jButton6.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton6ActionPerformed(evt);
            }
        });

        jButton2.setText("<");
        jButton2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton2ActionPerformed(evt);
            }
        });

        jButton3.setText(">");
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });

        jButton4.setText(">>");
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        tbldanh_sach.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null},
                {null, null, null, null}
            },
            new String [] {
                "Title 1", "Title 2", "Title 3", "Title 4"
            }
        ));
        tbldanh_sach.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                tbldanh_sachMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(tbldanh_sach);

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
        jPanel2.setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                        .addGap(0, 0, Short.MAX_VALUE)
                        .addComponent(jButton6)
                        .addGap(18, 18, 18)
                        .addComponent(jButton2)
                        .addGap(18, 18, 18)
                        .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(jButton3)
                        .addGap(18, 18, 18)
                        .addComponent(jButton4))
                    .addComponent(jScrollPane1))
                .addContainerGap())
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, jPanel2Layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 237, Short.MAX_VALUE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                .addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jButton6)
                    .addComponent(jButton2)
                    .addComponent(jButton3)
                    .addComponent(jButton4)
                    .addComponent(txtRows, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(20, 20, 20))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(25, 25, 25)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.CENTER)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(btnadd, javax.swing.GroupLayout.PREFERRED_SIZE, 106, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(21, 21, 21)
                        .addComponent(btnedit, javax.swing.GroupLayout.PREFERRED_SIZE, 85, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)
                        .addComponent(btnsave, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(13, 13, 13)
                        .addComponent(btndelete, javax.swing.GroupLayout.PREFERRED_SIZE, 75, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(14, 14, 14)
                        .addComponent(btnclose, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jLabel7)
                    .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(25, 25, 25))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addComponent(jLabel7, javax.swing.GroupLayout.PREFERRED_SIZE, 22, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(jPanel3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnedit, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnsave, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnclose, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btnadd, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(btndelete, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addGap(18, 18, 18)
                .addComponent(jPanel2, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGap(6, 6, 6))
        );

        pack();
        setLocationRelativeTo(null);
    }// </editor-fold>//GEN-END:initComponents

    private void btnaddActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnaddActionPerformed
        // TODO add your handling code here:
        this.add = true;
        this.edit = false;
        this.btnadd.setEnabled(false);
        this.btnedit.setEnabled(false);
        this.btnsave.setEnabled(true);
        this.clearText();
        defaultValue();
        this.setEditable(true);        
    }//GEN-LAST:event_btnaddActionPerformed

    private void btneditActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btneditActionPerformed
        // TODO add your handling code here:
        this.add = false;
        this.edit = true;
        this.btnadd.setEnabled(false);
        this.btnedit.setEnabled(false);
        this.btnsave.setEnabled(true);        
        this.setEditable(true);
        this.txtMaNV.setEditable(false);
    }//GEN-LAST:event_btneditActionPerformed

    private void btnsaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnsaveActionPerformed
        // TODO add your handling code here:
        Boolean flag = false;
        if(this.add){
           flag = this.addNew();
        }
        else if(this.edit){
           flag = EditRow();
        }
        if(flag == true){
            this.add = false;
            this.edit = false;
            this.btnadd.setEnabled(true);
            this.btnedit.setEnabled(true);
            this.btnsave.setEnabled(false);        
            this.setEditable(false);
        }
    }//GEN-LAST:event_btnsaveActionPerformed

    private void btndeleteActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btndeleteActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_btndeleteActionPerformed

    private void btncloseActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btncloseActionPerformed
        // TODO add your handling code here:
        this.setVisible(false);
    }//GEN-LAST:event_btncloseActionPerformed

    private void tbldanh_sachMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_tbldanh_sachMouseClicked
        // TODO add your handling code here:
        JTable table =(JTable)evt.getSource();
        Point p = evt.getPoint();
        int rowIndex = table.rowAtPoint(p);            
        if(rowIndex >=0){
            table.setRowSelectionInterval(rowIndex, rowIndex);
            this.currentIndex = rowIndex;
            this.view(rowIndex);
        }
    }//GEN-LAST:event_tbldanh_sachMouseClicked

    private void jButton2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton2ActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex > 0){
            this.currentIndex--;
            this.view(currentIndex);
        }
    }//GEN-LAST:event_jButton2ActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // TODO add your handling code here:
        if(this.currentIndex < this.numberRow){
            this.currentIndex++;
            this.view(currentIndex);
        }
    }//GEN-LAST:event_jButton3ActionPerformed

    private void jButton6ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton6ActionPerformed
        // TODO add your handling code here:
        this.currentIndex =0;
        this.view(currentIndex);
    }//GEN-LAST:event_jButton6ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        this.currentIndex = this.numberRow -1;
        this.view(currentIndex);
    }//GEN-LAST:event_jButton4ActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(FrmNhanVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(FrmNhanVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(FrmNhanVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(FrmNhanVien.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the form */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                new FrmNhanVien().setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnadd;
    private javax.swing.JButton btnclose;
    private javax.swing.JButton btndelete;
    private javax.swing.JButton btnedit;
    private javax.swing.JButton btnsave;
    private javax.swing.JComboBox cbbchuc_vu_id;
    private java.awt.Checkbox chkGioi_tinh;
    private com.toedter.calendar.JDateChooser dtcNgay_sinh;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton6;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JLabel jLabel18;
    private javax.swing.JLabel jLabel19;
    private javax.swing.JLabel jLabel20;
    private javax.swing.JLabel jLabel21;
    private javax.swing.JLabel jLabel22;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JPanel jPanel2;
    private javax.swing.JPanel jPanel3;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTable tbldanh_sach;
    private javax.swing.JTextField txtCmnd;
    private javax.swing.JTextField txtDiaChi;
    private javax.swing.JTextField txtDienThoai;
    private javax.swing.JTextField txtMaNV;
    private javax.swing.JTextField txtRows;
    private javax.swing.JTextField txtTenNV;
    // End of variables declaration//GEN-END:variables
}
